package com.jinxin.platform.cddjnhjcxtsysset.controller;

import com.jinxin.platform.base.api.annotation.APCOSApi;
import com.jinxin.platform.base.api.annotation.APCOSApiOperation;
import com.jinxin.platform.base.api.constant.APCOSApiConstans;
import com.jinxin.platform.base.common.pojo.JsonResult;
import com.jinxin.platform.cddjnhjcxtsysset.bean.CdcommondataTableInfo;
import com.jinxin.platform.cddjnhjcxtsysset.bean.CddjnhjcxtsyssetHomeDevice;
import com.jinxin.platform.cddjnhjcxtsysset.bean.CddjnhjcxtsyssetHomeReleva;
import com.jinxin.platform.cddjnhjcxtsysset.common.SysOrgInfo;
import com.jinxin.platform.cddjnhjcxtsysset.common.SysUserVO;
import com.jinxin.platform.cddjnhjcxtsysset.rpc.CddjnhjcxtsyssetRpc;
import com.jinxin.platform.cddjnhjcxtsysset.service.ICddjnhjcxtsyssetDeviceService;
import com.jinxin.platform.cddjnhjcxtsysset.service.ICddjnhjcxtsyssetHomeDeviceService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * All rights Reserved, Designed By www.beonelot.com
 *
 * @version: V1.0
 * @Copyright: www.beonelot.com Inc. All rights reserved.
 * @Description：
 * @Date: 2020-12-26
 * @Author: Lingnan
 **/
@APCOSApi(value = "首页关联设置相关接口", developers = {"lingnan"}, description = "首页关联设置相关接口")
@Api(tags = "首页关联设置相关接口")
@RestController
@RequestMapping("/cddjnhjcxtsysset/cddjnhjcxtsyssetHomeDevice")
@Slf4j
public class CddjnhjcxtsyssetHomeDeviceController {
    @Autowired
    private ICddjnhjcxtsyssetDeviceService cddjnhjcxtsyssetDeviceService;
    @Autowired
    private ICddjnhjcxtsyssetHomeDeviceService homeDeviceService;
    @Autowired
    private CddjnhjcxtsyssetRpc cddjnhjcxtsyssetRpc;

    @APCOSApiOperation(value = "根据条件查询设备列表", developers = {"lingnan"}, description = "根据条件查询设备列表",
            infType = APCOSApiConstans.INF_TYPE_OTHER, infCategory = APCOSApiConstans.INF_CATEGORY_ALL)
    @ApiOperation(value = "根据条件查询设备列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name="type",value="关联类型", paramType="query",dataType="String")
    })
    @GetMapping("/list")
    public Map<String,Object> list(@RequestParam() String type, @RequestParam(required = false) String nameOrCode,
                                   @RequestParam(required = false) String deviceType,
                                   @RequestParam(required = false) String otherType){
        SysUserVO currentUser = cddjnhjcxtsyssetRpc.getCurrentUser();
        Map<String,Object> resultMap = new HashMap<>();
        resultMap.put("code",0);
        if(StringUtils.isEmpty(type) || currentUser == null){
            resultMap.put("msg","无设备");
            resultMap.put("count",0);
            resultMap.put("data",null);
            return resultMap;
        }
        HashMap param = new HashMap();
        param.put("field01", type);
        param.put("nameOrCode", nameOrCode);
        param.put("field11", deviceType);
        param.put("field07", otherType);
        param.put("field30", currentUser.getOrgId());
        List<CdcommondataTableInfo> list = cddjnhjcxtsyssetDeviceService.list(param);
        resultMap.put("msg","success");
        resultMap.put("count",list.size());
        resultMap.put("data",list);
        return resultMap;
    }

    @APCOSApiOperation(value = "根据条件查询已选择设备列表", developers = {"lingnan"}, description = "根据条件查询已选择设备列表",
            infType = APCOSApiConstans.INF_TYPE_OTHER, infCategory = APCOSApiConstans.INF_CATEGORY_ALL)
    @ApiOperation(value = "根据条件查询已选择设备列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name="relevaId",value="关联ID", paramType="query",dataType="String")
    })
    @GetMapping("/listChoosed")
    public Map<String,Object> listChoosed(@RequestParam(required = false) String relevaId, @RequestParam(required = false) String nameOrCode){
        Map<String,Object> resultMap = new HashMap<>();
        resultMap.put("code",0);
        if(StringUtils.isEmpty(relevaId)){
            resultMap.put("msg","无选择的设备");
            resultMap.put("count",0);
            resultMap.put("data",null);
            return resultMap;
        }
        HashMap param = new HashMap();
        param.put("relevaId", relevaId);
        List<CddjnhjcxtsyssetHomeDevice> list = homeDeviceService.list(param);
        resultMap.put("msg","success");
        resultMap.put("count",list.size());
        resultMap.put("data",list);
        return resultMap;
    }

    @APCOSApiOperation(value = "删除设备", developers = {"lingnan"}, description = "删除设备",
            infType = APCOSApiConstans.INF_TYPE_OTHER, infCategory = APCOSApiConstans.INF_CATEGORY_ALL)
    @GetMapping("/delete")
    public JsonResult delete(String id,String type){
        return homeDeviceService.delete(id,type);
    }

    @APCOSApiOperation(value = "保存设备", developers = {"lingnan"}, description = "保存设备",
            infType = APCOSApiConstans.INF_TYPE_OTHER, infCategory = APCOSApiConstans.INF_CATEGORY_ALL)
    @PostMapping("/saveOrUpdate")
    public JsonResult saveOrUpdate(@RequestBody List<CddjnhjcxtsyssetHomeDevice> records){
        return homeDeviceService.saveOrUpdate(records);
    }

}
